package com.mumulx.check.service.service.impl;

import com.mumulx.check.mapper.UserInfoMapper;
import com.mumulx.check.model.UserInfo;
import com.mumulx.check.service.service.UserInfoService;
import com.mumulx.check.service.util.HelpDev;
import com.mumulx.check.service.util.RespBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("userInfoService")
@Slf4j
public class UserInfoServiceImpl implements UserInfoService , UserDetailsService {
    @Resource
    private UserInfoMapper userInfoMapper;

    public UserInfoMapper getUserInfoMapper() {
        return userInfoMapper;
    }

    public void setUserInfoMapper(UserInfoMapper userInfoMapper) {
        this.userInfoMapper = userInfoMapper;
    }

    @Resource
    HelpDev helpDev;

    /*客户端ajax方式提交添加用户信息*/
    @Override
    public void addUserInfo(UserInfo userInfo) {
        //性别处理
        String sex = userInfo.getUserSex();
        userInfo.setUserSex(sex.equals("1") ? "男" : "女");
        //密码加密
        String pwdMD5 = helpDev.md5Encode(userInfo.getUserPwd().getBytes());
        userInfo.setUserPwd(pwdMD5);
        userInfoMapper.addUserInfo(userInfo);
    }

    //根据手机号获取用户信息
    @Override
    public UserInfo getUserInfoByUserTel(String userTel) {
        return userInfoMapper.getUserInfoByUserTel(userTel);
    }

    /*根据用户id获取用户信息*/
    @Override
    public UserInfo getUserInfoByUserId(int UserId) {
        return userInfoMapper.getUserInfoByUserId(UserId);
    }

    //前台用户登录检查用户信息
    @Override
    public RespBean checkLogin(String userName, String password) {
        String pw = userInfoMapper.checkLogin(userName);

        //进行加密
        String pwdMD5 = helpDev.md5Encode(password.getBytes());
        if (pw == null) {
            //用户不存在
            //helpDev.setErrMessage("用户不存在");
            return RespBean.error("用户不存在");
        } else if (!pw.equals(pwdMD5)) {
            //密码错误
            return RespBean.error("密码错误");
        }
        return RespBean.ok();
    }


    @Override
    public void updateUserInfoByUserId(UserInfo userInfo) {
        String sex = userInfo.getUserSex();
        if (sex != null) {//用户对性别没有修改
            userInfo.setUserSex(sex.equals("1") ? "男" : "女");
        }
        String userPwd = userInfo.getUserPwd();

        //用户对密码进行了修改，进行加密
        if (userPwd != null && !userPwd.equals("")) {
            userPwd = helpDev.md5Encode(userPwd.getBytes());
            //userInfoMapper.updateUserInfo(userInfo);
        } else {
            //没进行密码修改给密码赋默认值，方便mybatis进行更新
            userPwd = null;
        }

        userInfo.setUserPwd(userPwd);
        userInfoMapper.updateUserInfoByUserId(userInfo);
    }


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserInfo userInfoByUserTel = userInfoMapper.getUserInfoByUserTel(username);
        if (userInfoByUserTel == null) {
            throw new UsernameNotFoundException("用户名不存在"+username);
        }

        /*//加载基础用户信息
        MyUserDetails myUserDetails = myUserDetailsServiceMapper.findByUserName(username);

        //加载用户角色列表
        List<String> roleCodes = myUserDetailsServiceMapper.findRoleByUserName(username);

        //通过用户角色列表加载用户的资源权限列表
        List<String> authorties = myUserDetailsServiceMapper.findAuthorityByRoleCodes(roleCodes);

        //角色是一个特殊的权限，ROLE_前缀
        roleCodes = roleCodes.stream()
                .map(rc -> "ROLE_" + rc)
                .collect(Collectors.toList());
        authorties.addAll(roleCodes);
        myUserDetails.setAuthorities(
                AuthorityUtils.commaSeparatedStringToAuthorityList(
                        String.join(",", authorties)
                )
        );*/


        log.info("----------用户登录了"+userInfoByUserTel);
        return userInfoByUserTel;

    }
}